package com.example.demo.controller;

import com.example.demo.bean.ExcelData;
import com.example.demo.util.ExcelUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFCreationHelper;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class ExcelExportController {

    @GetMapping("/export/excel-with-image")
    public void exportExcelWithImage(HttpServletResponse response) throws IOException {
        String filePath = "C:\\Users\\zsn\\Pictures\\Feedback\\{4F00CDD9-BC07-45B9-9BBE-38898B83DE7C}\\1.png"; // 替换为你的文件路径

        try (FileInputStream fis = new FileInputStream(filePath);
             ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

            byte[] buffer = new byte[1024]; // 缓冲区大小可以根据需要调整
            int bytesRead;

            // 使用循环读取文件内容到缓冲区，直到文件末尾
            while ((bytesRead = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead); // 将缓冲区中的数据写入ByteArrayOutputStream
            }

            // 将ByteArrayOutputStream转换为字节数组
            byte[] fileContent = baos.toByteArray();
            ExcelData excelData = new ExcelData();
            List<List<Object>> list1 = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                List<Object> list = new ArrayList<>();
                list.add("张三");
                list.add("男");
                list.add(fileContent);
                list1.add(list);
            }
            List<String> titles = new ArrayList<>();
            titles.add("姓名");
            titles.add("性别");
            titles.add("头像");
            excelData.setName("test");
            excelData.setRows(list1);
            excelData.setTitles(titles);

            ExcelUtils.exportExcel(response, "123.xlsx", excelData);
        }
    }
}